home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <net/if.h>
- #include <netinet/if_ether.h>
- #include <sys/if_ax.h>
-
- u_char ax25broadcastaddr[7] = {
- 'Q'<<1, 'S'<<1, 'T'<<1, ' '<<1, ' '<<1, ' '<<1, '0'<<1
- };
- static struct ether_addr etherbroadcastaddr = {
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- };
- static struct ether_addr ethernulladdr;
-
- /*
- * convert an ax25 address to an ether_addr
- */
- ax_ax2ether(axhp, ethp)
- u_char *axhp;
- struct ether_addr *ethp;
- {
- u_char c;
- int i;
-
- for(i=0; i<6; i++)
- printf("%02x.", axhp[i]);
- printf("%02x '", axhp[6]);
-
- for(i=0; i<(7-1); i++)
- printf("%c", axhp[i]>>1);
- printf("-%c' == ", axhp[6]>>1);
-
- if( !bcmp((caddr_t)axhp, (caddr_t)ax25broadcastaddr,
- sizeof(ax25broadcastaddr)) ) {
- bcopy((caddr_t)ðerbroadcastaddr, (caddr_t)ethp,
- sizeof(struct ether_addr));
- goto done;
- }
-
- ethp->ether_addr_octet[0] = 0x99;
- c = ((((axhp[0]>>1) - ' ') << 2) & 0xfc);
- c |= ((((axhp[1]>>1) - ' ') >> 4) & 0x03);
- ethp->ether_addr_octet[1] = c;
- c = ((((axhp[1]>>1) - ' ') << 4) & 0xf0);
- c |= ((((axhp[2]>>1) - ' ') >> 2) & 0x0f);
- ethp->ether_addr_octet[2] = c;
- c = ((((axhp[2]>>1) - ' ') << 6) & 0xc0);
- c |= ((((axhp[3]>>1) - ' ') ) & 0x3f);
- ethp->ether_addr_octet[3] = c;
- c = ((((axhp[4]>>1) - ' ') << 2) & 0xfc);
- c |= ((((axhp[5]>>1) - ' ') >> 4) & 0x03);
- ethp->ether_addr_octet[4] = c;
- c = ((((axhp[5]>>1) - ' ') << 4) & 0xf0);
- c |= ((((axhp[6]>>1) - '0') ) & 0x0f);
- ethp->ether_addr_octet[5] = c;
-
- done:
- for(i=0; i<sizeof(struct ether_addr)-1; i++)
- printf("%02x:", ethp->ether_addr_octet[i]);
- printf("%02x\n", ethp->ether_addr_octet[5]);
- return 0;
- }
-
- /*
- * convert an ether_addr to an ax25 address
- */
- ax_ether2ax(ethp, axhp)
- struct ether_addr *ethp;
- u_char *axhp;
- {
- u_char c;
- int i;
-
- for(i=0; i<sizeof(struct ether_addr)-1; i++)
- printf("%02x:", ethp->ether_addr_octet[i]);
- printf("%02x == ", ethp->ether_addr_octet[5]);
-
- if( !bcmp((caddr_t)ethp, (caddr_t)ðernulladdr,
- sizeof(struct ether_addr))) {
- printf("nothing\n");
- return;
- }
- if( !bcmp((caddr_t)ethp, (caddr_t)ðerbroadcastaddr,
- sizeof(struct ether_addr))) {
- axhp[0] = 'Q' << 1;
- axhp[1] = 'S' << 1;
- axhp[2] = 'T' << 1;
- axhp[3] = ' ' << 1;
- axhp[4] = ' ' << 1;
- axhp[5] = ' ' << 1;
- axhp[6] = '0' << 1;
- goto done;
- }
-
- c = ethp->ether_addr_octet[0];
- if(c != 0x99) {
- printf("[NO MAPPING]\n");
- for(i=0; i<7; i++)
- axhp[i] = (i+1) << 1;
- return;
- }
- c = (ethp->ether_addr_octet[1] >> 2) & 0x3f;
- axhp[0] = (u_char)((c + ' ') << 1);
- c = (ethp->ether_addr_octet[1] << 4) & 0x30;
- c |= (ethp->ether_addr_octet[2] >> 4) & 0x0f;
- axhp[1] = (u_char)((c + ' ') << 1);
- c = (ethp->ether_addr_octet[2] << 2) & 0x3c;
- c |= (ethp->ether_addr_octet[3] >> 6) & 0x03;
- axhp[2] = (u_char)((c + ' ') << 1);
- c = (ethp->ether_addr_octet[3] ) & 0x3f;
- axhp[3] = (u_char)((c + ' ') << 1);
- c = (ethp->ether_addr_octet[4] >> 2) & 0x3f;
- axhp[4] = (u_char)((c + ' ') << 1);
- c = (ethp->ether_addr_octet[4] << 4) & 0x30;
- c |= (ethp->ether_addr_octet[5] >> 4) & 0x0f;
- axhp[5] = (u_char)((c + ' ') << 1);
- c = (ethp->ether_addr_octet[5] ) & 0x0f;
- axhp[6] = (u_char)((c + '0') << 1);
-
- done:
- for(i=0; i<6; i++)
- printf("%02x.", axhp[i]);
- printf("%02x '", axhp[6]);
-
- for(i=0; i<6; i++)
- printf("%c", (axhp[i]>>1) & 0x7f );
- printf("-%c'\n", (axhp[6]>>1) & 0x7f );
- }
-
- main(argc, argv)
- char **argv;
- {
- u_char ax25[7];
- struct ether_addr eth;
- u_char *cp, c;
- int i;
-
- if(argc<3) {
- fprintf(stderr, "usage: %s af addr\n", argv[0]);
- fprintf(stderr, "\taf=ax25, ether\n");
- exit(0);
- }
-
- if( !strcmp(argv[1], "ax25")) {
- i = 0;
- bzero((caddr_t)ax25, sizeof(ax25));
- for(cp=(u_char *)argv[2]; *cp && (i<6); cp++) {
- if(*cp=='-') {
- cp++;
- break;
- }
- c = islower(*cp) ? toupper(*cp) : *cp;
- ax25[i++] = (u_char)(c << 1);
- }
- while(i<6)
- ax25[i++] = (u_char)(' ' << 1);
- while(*cp=='-')
- cp++;
- if(*cp)
- ax25[6] = (u_char)(*cp << 1);
- ax_ax2ether(ax25, ð);
-
- } else if( !strcmp(argv[1], "ether")) {
- bcopy((caddr_t)ether_aton(argv[2]), (caddr_t)ð,
- sizeof(struct ether_addr));
- ax_ether2ax(ð, ax25);
- } else
- printf("what?\n");
- return 0;
- }
-